//
//  ShopOrderViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/18.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "ShopOrderViewController.h"
#import "DataSelectView.h"
#import "ConsumptionStateView.h"
#import "FinancialSecitonHeaderView.h"
#import "ShopOrderListCell.h"
#import "ShopWordsCell.h"
#import "ShopLogisticsInfoCell.h"
#import "TheOrderDetailsViewController.h"
@interface ShopOrderViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;

@property (nonatomic,strong) UILabel *startLabel;
@property (nonatomic,strong) UILabel *endLabel;

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *logisticsDataArray;    //物流信息
@property (nonatomic,strong) NSDictionary *dataDictionary;
Strong UIButton *typeButton; /**<*/
Assign int pstart;

@end

//网络
@implementation ShopOrderViewController (NetWorking)

/**
 获取页面数据

 @param pstart 页数
 */
- (void)getData:(NSString *)pstart{
    [self showprogressHUD];
    if ([[UserModel sharedInstanced].s_id isEqualToString:@""]) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hiddenProgressHUD];
        return;
    }
    NSDictionary *parameters = @{@"s_id":[NSString stringWithFormat:@"%@",[UserModel sharedInstanced].s_id],
                                 @"psize":@"20",
                                 @"pstart":pstart};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/OrderInformationMoblie/selectSid.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            if ([pstart isEqualToString:@"0"]) {
               [self.dataArray removeAllObjects];
                self.pstart = 0;
            }
            self.pstart ++;
            [self.dataArray addObjectsFromArray:responseObject[@"obj"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

/**
 修改进入店铺状态

 @param o_id o_id description
 */
- (void)getUpdateConsume:(NSString *)o_id{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"o_id":o_id};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/OrderInformationMoblie/updateConsume.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        ShowMessage(responseObject[@"msg"]);
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            [self getData:@"0"];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

/**
 通过日期查询
 */
- (void)getSelByTimeData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"s_id":[UserModel sharedInstanced].userId,
                                 @"first_time":self.startLabel.text,
                                 @"tail_time":self.endLabel.text};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/OrderInformationMoblie/selByTime.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            [self.dataArray removeAllObjects];
            self.pstart = 0;
            self.pstart ++;
            [self.dataArray addObjectsFromArray:responseObject[@"obj"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

//UI
@implementation ShopOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"订单管理";
    [self.view addSubview:self.tableView];
    [self getData:@"0"];
    
}

- (void)timeLabelEvent:(UITapGestureRecognizer *)sender{
    DataSelectView *view = [[DataSelectView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [view show];
    
    view.selectHandle = ^(NSString *str) {
        ((UILabel *)sender.view).text = str;
        [self getSelByTimeData];
    };
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40 / WIDTH_5S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    if ([self.dataArray[section][@"o_state"] isEqualToString:@"1"]) {
//        return 45 / WIDTH_5S_SCALE;
//    }
    return 5 / WIDTH_5S_SCALE;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    FinancialSecitonHeaderView *view = [[FinancialSecitonHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40 / WIDTH_5S_SCALE)];
    [view setTitle:@"订单编号" content:@"565775568913" drawState:NO];
    
    return view;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
//    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6+self.logisticsDataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if(indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 ){
        return kShopOrderListCellHeight;
    }else if(indexPath.row == 2){
        return [ShopWordsCell getCellHeight:@"买家留言酒宴留言"];
    }else{
        return 60 / WIDTH_5S_SCALE;
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *listIndetifier = @"listIdentifier";
    ShopOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:listIndetifier];
    if(!cell){
        cell = [[ShopOrderListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:listIndetifier];
    }
    if(indexPath.row == 0){

        [cell setTitle:@"用户名\n手机号" content:@"张三\n13200009999" titleColor:[UIColor blackColor] contentColor:[UIColor blackColor]];
        return cell;
    }else if(indexPath.row == 1){

        [cell setTitle:@"订单金额" content:@"100.00" titleColor:[UIColor blackColor] contentColor:getColor(mainColor)];
        return cell;
    }else if (indexPath.row == 2){
        ShopWordsCell *wordCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ShopWordsCell class])];
        if(!wordCell){
            wordCell = [[ShopWordsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([ShopWordsCell class])];
        }
        [wordCell setWordsString:@"买家留言酒宴留言"];
        return wordCell;
    }else if (indexPath.row == 3){

        [cell setTitle:@"快递员" content:nil titleColor:getColor(mainColor) contentColor:nil];
        return cell;
    }else if (indexPath.row == 4){

        [cell setTitle:@"小美丽（561M）" content:@"15255984456" titleColor:[UIColor blackColor] contentColor:getColor(mainColor)];
        return cell;
    }else if (indexPath.row == 5){

        [cell setTitle:@"订单状态" content:nil titleColor:getColor(mainColor) contentColor:nil];
        return cell;
    }else {
        ShopLogisticsInfoCell *logistcsCell = [ShopLogisticsInfoCell cellForTableView:tableView];
        logistcsCell.indexPath = indexPath;
        logistcsCell.model = self.logisticsDataArray[indexPath.row-6];
        //cell.accessoryType = indexPath.row == 3 ?  UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
        return logistcsCell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TheOrderDetailsViewController *vc = [TheOrderDetailsViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - set & get

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 5;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
//        _tableView.rowHeight = 180;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;

        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getData:@"0"];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self getData:[NSString stringWithFormat:@"%d",self.pstart]];
        }];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 36)];
        
        _startLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH/2 - 100)/2, 8, 100, 20)];
        _startLabel.backgroundColor = [UIColor whiteColor];
        _startLabel.layer.masksToBounds = YES;
        _startLabel.layer.cornerRadius = 10;
        _startLabel.text = @"--请选择日期--";
        _startLabel.font = DEF_FontSize_12;
        _startLabel.textAlignment = NSTextAlignmentCenter;
        _startLabel.userInteractionEnabled = YES;
        [_startLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeLabelEvent:)]];
        
        _endLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2.+(SCREEN_WIDTH/2 - 100)/2, 8, 100, 20)];
        _endLabel.backgroundColor = [UIColor whiteColor];
        _endLabel.layer.masksToBounds = YES;
        _endLabel.layer.cornerRadius = 10;
        _endLabel.text = @"--请选择日期--";
        _endLabel.font = DEF_FontSize_12;
        _endLabel.textAlignment = NSTextAlignmentCenter;
        _endLabel.userInteractionEnabled = YES;
        [_endLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeLabelEvent:)]];
        
        [_headerView addSubview:_startLabel];
        [_headerView addSubview:_endLabel];
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = @{};
    }
    return _dataDictionary;
}

#pragma mark - data

- (NSMutableArray *)logisticsDataArray{
    if (!_logisticsDataArray) {
        _logisticsDataArray  = @[@{@"title":@"卖家发货",@"time":@"2017-09-08 12:33:11"}.mutableCopy,
                        @{@"title":@"由广东物流公司发往沈阳中转站",@"time":@"2017-09-08 12:33:11"}.mutableCopy
                        ,
                        @{@"title":@"辽宁沈阳总部收入",@"time":@"2017-09-08 12:33:11"}.mutableCopy
                        ,
                        @{@"title":@"辽宁沈阳浑南第五分部已收入",@"time":@"2017-09-08 12:33:11",@"phone":@"15542914939"}.mutableCopy
                        ,
                        @{@"title":@"客户已经签收",@"time":@"2017-09-08 12:33:11"}.mutableCopy
                        ].mutableCopy;
    }
    return _logisticsDataArray;
}

@end
